package com.hyzx.qbasic.admin.controller.process;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.po.ProcessDefinitionPagedInfoPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.model.vo.res.admin.GetProcessDefinitionInfoRes;
import com.hyzx.qbasic.admin.model.vo.res.admin.GetProcessStepsRes;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.IProcessDefinitionService;
import com.hyzx.qbasic.admin.domain.service.IProcessFacadeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 黄江华 on 2018/11/22
 */


@Api(tags = "流程定义", description = "ProcessDefinitionController")
@Controller
@RequestMapping("/process/definition")
public class ProcessDefinitionController extends BaseController {

    @Autowired
    private IProcessDefinitionService definitionService;

    @Autowired
    private IProcessFacadeService processFacadeService;

    @ApiIgnore
    @SysLog("流程定义首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.PROCESS_DEFINITION_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.PROCESS_DEFINITION_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.PROCESS_DEFINITION_DELETE));
        return "process/definition/list";
    }

    @ApiOperation("获取流程定义列表")
    @GetMapping("/getProcessDefinitionList")
    @ResponseBody
    public BaseResModel<PageInfo<ProcessDefinitionPagedInfoPO>> getProcessDefinitionList(@Valid GetProcessDefinitionListReq req, BindingResult result) {
        return BaseResUtils.success(definitionService.getPagedProcessDefinitionList(req));
    }

    @ApiOperation("获取流程定义列表")
    @GetMapping("/getProcessDefinitionListByCategoryId")
    @ResponseBody
    public BaseResModel<List<ProcessDefinitionPagedInfoPO>> getProcessDefinitionListByCategoryId(@Valid GetProcessDefinitionListByCategoryIdReq req, BindingResult result) {
        return BaseResUtils.success(definitionService.getProcessDefinitionListByCategoryId(req.getProcessCategoryId()));
    }

    @ApiIgnore
    @SysLog("新增流程定义页")
    @GetMapping("/add")
    public String add() {
        return "process/definition/add";
    }

    @ApiOperation("新增流程定义表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddProcessDefinitionReq req, BindingResult result) {
        BaseOperationRes res = definitionService.addProcessDefinition(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("删除流程定义")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteProcessDefinitionReq req, BindingResult result) {
        BaseOperationRes res = definitionService.deleteProcessDefinition(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("编辑流程定义页")
    @GetMapping("/edit")
    public String edit() {
        return "process/definition/edit";
    }

    @ApiOperation("编辑流程定义表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditProcessDefinitionReq req, BindingResult result) {
        BaseOperationRes res = definitionService.editProcessDefinition(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取流程定义信息")
    @GetMapping("/getProcessDefinitionInfo")
    @ResponseBody
    public BaseResModel<GetProcessDefinitionInfoRes> getProcessDefinitionInfo(@Valid GetProcessDefinitionInfoReq req, BindingResult result) {
        return BaseResUtils.success(definitionService.getProcessDefinitionInfo(req.getProcessDefinitionId()));
    }

    @ApiIgnore
    @SysLog("查看流程")
    @GetMapping("/processSteps")
    public String processSteps() {
        return "process/definition/processSteps";
    }

    @ApiOperation("获取流程节点信息")
    @GetMapping("/getProcessSteps")
    @ResponseBody
    public BaseResModel<GetProcessStepsRes> getProcessSteps(@Valid GetProcessDefinitionInfoReq req, BindingResult result) {
        GetProcessStepsRes res = definitionService.getProcessSteps(req.getProcessDefinitionId());
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("查看流程实例")
    @GetMapping("/processInstanceSteps")
    public String processInstanceSteps() {
        return "process/definition/processInstanceSteps";
    }

    @ApiOperation("获取流程实例节点信息")
    @GetMapping("/getProcessInstanceSteps")
    @ResponseBody
    public BaseResModel<GetProcessStepsRes> getProcessInstanceSteps(@Valid GetProcessInstanceStepsReq req, BindingResult result) {
        GetProcessStepsRes res = processFacadeService.getProcessSteps(req.getBusinessKey());
        return BaseResUtils.success(res);
    }
}
